package top.lshaci.learning.activiti.day2;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Assert;
import org.junit.Test;

import static top.lshaci.learning.activiti.util.ProcessEngineSingleton.singleton;

/**
 * PublicTaskTest
 * <p>用户公有任务测试</p>
 * <pre>
 * </pre>
 *
 * @author lshaci
 * @since 1.0.0
 */
public class PublicTaskTest {

    @Test
    public void test() {
        // 1.部署流程
        Deployment deployment = singleton.deploy("PublicTask");
        System.err.println(deployment);
        // 2.启动流程
        ProcessInstance processInstance = singleton.start("PublicTask");
        String processInstanceId = processInstance.getId();
        System.err.println("processInstanceId: " + processInstanceId);
        // 3.验证流程的状态（位置）
        // 获取当前流程 "用户取号" 节点对应的Task对象
        Task t1 = singleton.taskService()
                .createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskAssignee("小强")
                .singleResult();
        System.err.println("t1: " + t1);
        Assert.assertNotNull(t1);

        singleton.taskService().complete(t1.getId());

        // 获取当前流程 "业务员办理" 节点对应的Task对象
        Task t2 = singleton.taskService()
                .createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskCandidateOrAssigned("营业员A")
                .singleResult();
        System.err.println("t2: " + t2);
        Assert.assertNotNull(t2);
        Task t3 = singleton.taskService()
                .createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskCandidateOrAssigned("营业员B")
                .singleResult();
        System.err.println("t3: " + t3);
        Assert.assertNotNull(t3);
        // 认领任务
        singleton.taskService().claim(t2.getId(), "营业员A");
        // 再次查询"营业员B"则为空
        Task t4 = singleton.taskService()
                .createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskCandidateOrAssigned("营业员B")
                .singleResult();
        System.err.println("t4: " + t4);
        Assert.assertNull(t4);
        singleton.taskService().complete(t2.getId());
        // 验证流程结束
        ProcessInstance dbProcessInstance = singleton.runtimeService()
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        Assert.assertNull(dbProcessInstance);
    }
}
